package com.quanyan.authority.service.impl;

import com.lefu.commons.utils.Page;
import com.quanyan.authority.mapper.AuthorityMapper;
import com.quanyan.authority.mapper.TbAuthorityMapper;
import com.quanyan.authority.model.db.TbAuthority;
import com.quanyan.authority.model.db.TbFunction;
import com.quanyan.authority.model.request.ReqBaseParam;
import com.quanyan.authority.model.request.ReqPage;
import com.quanyan.authority.service.AuthorityService;
import com.quanyan.common.constants.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/7/28.
 */
@Service
public class AuthorityServiceImpl implements AuthorityService {

    private static final Logger logger = LoggerFactory.getLogger(AuthorityServiceImpl.class);

    @Autowired
    private TbAuthorityMapper tbAuthorityMapper;

    @Autowired
    private AuthorityMapper authorityMapper;

    @Override
    public TbAuthority findById(Integer id) {
        if(id != null)
            return tbAuthorityMapper.selectByPrimaryKey(id);
        return null;
    }

    @Override
    public TbAuthority add(ReqBaseParam reqBaseParam) {
        TbAuthority tbAuthority = new TbAuthority();
        tbAuthority.setName(reqBaseParam.getName());
        tbAuthority.setCode(reqBaseParam.getCode());
        tbAuthority.setFounder(reqBaseParam.getFounder());
        tbAuthority.setIsDelete(Constants.IS_DELETE_FALSE);
        tbAuthority.setCreateTime(new Date());
        tbAuthority.setUpdateTime(new Date());
        tbAuthorityMapper.insertSelective(tbAuthority);
        return tbAuthority;
    }

    @Override
    public void del(Integer id) {
        TbAuthority tbAuthority = new TbAuthority();
        tbAuthority.setId(id);
        tbAuthority.setIsDelete(Constants.IS_DELETE_TRUE);
        tbAuthorityMapper.updateByPrimaryKeySelective(tbAuthority);
    }

    @Override
    public void update(ReqBaseParam reqBaseParam) {
        TbAuthority tbAuthority = new TbAuthority();
        tbAuthority.setId(reqBaseParam.getId());
        tbAuthority.setName(reqBaseParam.getName());
        tbAuthority.setCode(reqBaseParam.getCode());
        tbAuthority.setRemark(reqBaseParam.getRemark());
        tbAuthorityMapper.updateByPrimaryKeySelective(tbAuthority);
    }

    @Override
    public Page findAll(ReqPage reqPage, ReqBaseParam reqBaseParam) {
        Page page = new Page();
        page.setCurrentPage(reqPage.getPageNum());
        page.setShowCount(reqPage.getPageSize());
        List<TbAuthority> lst = authorityMapper.findAll(page, reqBaseParam.toMap());
        page.setObject(lst);
        logger.info("{}",page.toString());
        return page;
    }
}
